package com.cskaoyan.app.controller;

import com.cskaoyan.app.bean.bo.adminbo.AdminAdminBo;
import com.cskaoyan.app.bean.bo.adminbo.AdminDelBo;
import com.cskaoyan.app.bean.common.BasePageInfo;
import com.cskaoyan.app.bean.common.BaseRespVo;
import com.cskaoyan.app.bean.common.CommonData;
import com.cskaoyan.app.bean.po.MarketStorage;
import com.cskaoyan.app.config.AliComponent;
import com.cskaoyan.app.service.AdminStorageService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @author kiggy
 * @since 2022/12/29 11:42
 */
@RestController
@RequestMapping("admin/storage")
@MultipartConfig
public class AdminStorageController {

    @Value("${spring.web.resources.static-locations}")
    private String filePath;

    @Autowired
    AdminStorageService adminStorageService;

    @Autowired
    AliComponent aliComponent;

    @GetMapping("list")
    public BaseRespVo list(BasePageInfo basePageInfo) {
        // 获得分页信息
        // PageHelper.startPage(1,20);
        PageInfo<MarketStorage> adminPageInfo = adminStorageService.listPageAdmins(basePageInfo);

        CommonData<MarketStorage> marketAdminCommonData = CommonData.data(adminPageInfo);

        return BaseRespVo.ok(marketAdminCommonData);

    }

    @PostMapping("create")
    public BaseRespVo create(MultipartFile file) throws IOException {
        // String name = file.getName();
        try {
            String filename = file.getOriginalFilename();
            String type = filename.substring(filename.lastIndexOf('.') + 1);
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            String name = dateFormat.format(new Date()) + "." + type;
            InputStream inputStream = file.getInputStream();
            aliComponent.fileUpload(name, inputStream);
            String url = aliComponent.getUrl(name);
            MarketStorage marketStorage = adminStorageService.createStorageData(url, file, name);
            return BaseRespVo.ok(marketStorage);
        } catch (MaxUploadSizeExceededException e) {
            return BaseRespVo.invalidParameter("文件过大！");
        }

    }

    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody AdminDelBo adminDelBo) {
        adminStorageService.delAdminData(adminDelBo);
        return BaseRespVo.ok(null);
    }

    /**
     * 接收上传图片
     * abandon
     *
     * @param request
     * @return
     */
    @PostMapping("createLoacal")
    public BaseRespVo create(HttpServletRequest request) throws IOException, ServletException {
        // 创建文件
        Part part = request.getPart("file");
        String[] split = part.getSubmittedFileName().split("\\.");
        String filetype = split[split.length - 1];
        // String fileName = part.getSubmittedFileName();
        // String fileName = UUID.randomUUID().toString();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = new Date();
        String format = dateFormat.format(date);
        String fileName = format + "." + filetype;
        InputStream inputStream = part.getInputStream();

        // 获得其路径
        String replace = filePath.replace("file:", "");

        File file = new File(replace + "\\" + fileName);
        if (!file.exists()) {
            File parentFile = file.getParentFile();
            parentFile.mkdirs();
        }
        file.createNewFile();
        // 下载文件
        FileOutputStream outputStream = new FileOutputStream(file);
        byte[] bytes = new byte[1024];
        int len = 0;
        while ((len = inputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, len);
        }
        // 获得marketAdmin
        MarketStorage adminCreateVo = adminStorageService.createStorageData(file);

        return BaseRespVo.ok(adminCreateVo);
    }

}
